#!/usr/local/bin/php
<?php
require_once 'common.php';

global $logger;
global $dbOAIICust;
global $dbOAIICode;
global $codeModel;

$sql = "SELECT SP_ID,STATUS,SP_NAME,SP_CODE,NOTES,
	TO_CHAR(UPDATETIME, 'YYYY-MM-DD HH24:MI:SS') AS UPDATETIME 
	FROM TBCODESP ORDER BY SP_CODE";
$st = oci_parse($dbOAIICust, $sql);
if (!oci_execute($st)) {
    $e = oci_error($st);
    $logger->err("oci_execute failed: code-{$e['code']} message-{$e['message']} sql-{$e['sqltext']}");
    return false;
}
$sps = array();
oci_fetch_all($st, $sps, null, null, OCI_FETCHSTATEMENT_BY_ROW + OCI_ASSOC);
foreach ($sps as $sp) {
    $companyId = (int) $sp['SP_CODE'];   
    $company = $codeModel->getCompany($companyId, OaAppModel::APP_CONFIG_ENV_PRODUCTION);
    if ($company && $company['SP_NAME'] != $sp['SP_NAME']) {
        $logger->err("company name conflicted: company-$companyId code_name-{$company['SP_NAME']} cust_name-{$sp['SP_NAME']}");
    }
    $company = $codeModel->getCompanyByName($sp['SP_NAME'], OaAppModel::APP_CONFIG_ENV_PRODUCTION);
    if ($company && $company['SP_ID'] != $sp['SP_CODE']) {
        $logger->err("company id conflicted: company-{$sp['SP_NAME']} code_id-{$company['SP_ID']} cust_id-{$sp['SP_CODE']}");
    }
}
